package coreapi;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 演示如何自己实现 GenericServlet
 */
public abstract class MyGenericServlet implements Servlet, ServletConfig {

    //======================实现Servlet接口======================
    ServletConfig config;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.config = servletConfig;
        init();
    }

    /**
     * 为了保证能正确初始化，应该覆盖这个方法
     */
    public void init() {

    }

    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    @Override
    public abstract void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException;

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }

    //======================实现ServletConfig接口======================

    @Override
    public String getServletName() {
        return getServletConfig().getServletName();
    }

    @Override
    public ServletContext getServletContext() {
        return getServletConfig().getServletContext();
    }

    @Override
    public String getInitParameter(String s) {
        return getServletConfig().getInitParameter(s);
    }

    @Override
    public Enumeration<String> getInitParameterNames() {
        return getServletConfig().getInitParameterNames();
    }
}
